package com.comprehensive.apigateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class LoggingGlobFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String path = exchange.getRequest().getPath().value();
        String method = exchange.getRequest().getMethod().toString();
        String routeId = exchange.getAttribute("org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ID");

        // 获取实际转发的目标地址
        String targetUri = exchange.getRequest().getURI().toString();

        System.out.printf("[Gateway Request] Method: %s | Path: %s | RouteID: %s | Forwarded to: %s%n",
                method, path, routeId, targetUri);
        return chain.filter(exchange);
    }
}
